<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/ajax.js"></script>
</head>

<body>

</body>
<script>
    function readText(url) {
        var p = new Promise(function (resolve, reject) {
            $.ajax({
                type: "get",
                url: url,
                success: function (text) {
                    resolve(text);
                },
                error: function (err) {
                    reject(err);
                }
            })
        })
        return p; // {pending}
    }

    // ajax恐怖回调的解决方式1:
    // T = T1 + T2 + T3   
    var str = "";
    readText("../data/1.txt").then(txt => {
        console.log("line 36", txt);
        str += txt;
        return readText("../data/2.txt");
    }).then(txt => {
        console.log("line 40", txt);
        str += txt;
        return readText("../data/3.txt");
    }).then(txt => {
        console.log("line 44", txt);
        str += txt;
        return str;
    }).then(txt => {
        console.log("line 48", txt);
    }).catch(err => {
        console.log("失败:", err);
    })

</script>

</html>